package com.ccg.controller;

import com.ccg.entity.Message;
import com.ccg.entity.Order;
import com.ccg.entity.RoomType;
import com.ccg.entity.User;
import com.ccg.service.OrderService;
import com.ccg.service.RoomTypeService;
import com.ccg.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.text.ParseException;

@Controller
@RequestMapping("/common")
public class CommonController {
    @Autowired
    UserService userService;
    @Autowired
    OrderService orderService;
    @Autowired
    RoomTypeService roomTypeService;

    //用户修改密码
    @RequestMapping("/updatePasswByUsername")
    @ResponseBody
    public Message updatePasswByUsername(@RequestBody User user) {
        UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        user.setUsername(userDetails.getUsername());
        return userService.updatePasswByUsername(user);
    }
    //用户修改邮箱地址
    @RequestMapping("/updateEmail")
    @ResponseBody
    public Message updateEmail(@RequestBody User user) {
        UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        user.setUsername(userDetails.getUsername());
        return userService.updateEmail(user);
    }
    //修改用户真实姓名
    @RequestMapping("/updateRealName")
    @ResponseBody
    public Message updateRealName(@RequestBody User user) {
        if(user.getRealname()!=""&&user.getRealname()!=null) {
            UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            user.setUsername(userDetails.getUsername());
            return userService.updateRealName(user);
        }
        else return new Message(1,"修改真实姓名失败","真实姓名不能为空");
    }


    //返回当前用户的所有订单
    @RequestMapping("/allOrdersByU_id")
    @ResponseBody
    public Message allOrdersByU_id(){
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return new Message(0,"查找订单成功",orderService.findOrderByName(user.getU_id()));
    }
    //用户下订单
    @RequestMapping("/addOrderByU_id")
    @ResponseBody
    public Message addOrderByU_id(@RequestBody Order order) throws ParseException {
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        order.setU_id(user.getU_id());//获取当前用户的id
        return orderService.addOrderByU_id(order);
    }
    //用户取消订单
    @RequestMapping("/orderCancelByUser")
    @ResponseBody
    public Message orderCancelByUser(Order order){
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        order.setU_id(user.getU_id());//获取当前用户的id
        return orderService.orderCancelByUser(order);
    }
    //用户删除订单
    @RequestMapping("/deleteOrder")
    @ResponseBody
    public Message deleteOrder(Order order){
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        order.setU_id(user.getU_id());//获取当前用户的id
        int i = orderService.deleteOrder(order);
        if(i==1)return new Message(0,"删除订单成功",null);
        else return new Message(1,"删除订单失败","异常错误");
    }
    //用户获取某一房间类型以及预订资源库存
    @RequestMapping("/getDateRoomNumber")
    @ResponseBody
    public Message getDateRoomNumber(int rt_id){
        RoomType roomType = roomTypeService.roomTypeDateNumber(rt_id);
        if(roomType==null||roomType.getDate_rt_number().size()==0||roomType.getDate_rt_number()==null)
            return new Message(1,"获取数据异常",null);
        return new Message(0,"获取数据成功",roomType);
    }

    @RequestMapping("/test")
    @ResponseBody
    public Object test(){
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        user.setPassword("");
        return user;
    }


}
